
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>端口扫描任务管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>扫描任务搜索</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">任务名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="cusname" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"><i class="layui-icon">&#xe624;</i>添加任务</button>
                <button class="layui-btn layui-btn-sm" lay-event="Refresh"><i class="layui-icon">&#xe666;</i>刷新</button>
                <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="empty"><i class="layui-icon">&#xe640;</i>清空数据</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTable_portscanmanager" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs" lay-event="show"><i class="layui-icon">&#xe642;</i>查看详情</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"><i class="layui-icon">&#xe640;</i>删除任务</a>
        </script>

        <hr class="layui-bg-blue">

        <fieldset class="table-search-fieldset">
            <legend>分布式端口扫描消息队列详情</legend>
            <div style="margin: 10px 10px 10px 10px">

                <script type="text/html" id="toolbarPortScanNsqd">
                    <div class="layui-btn-container">
                        <span class="layui-badge layui-bg-orange" id="message_count">消息总数：0</span>
                        <span class="layui-badge layui-bg-green" id="message_bytes">消息大小：0 byte</span>
                        <span class="layui-badge layui-bg-blue" id="client_count">客户端：0个</span>
                        <span class="layui-badge layui-bg-cyan id=timeout_count" id="timeout_count">消息超时：0个</span>
                    </div>
                </script>

                <table class="layui-hide" id="currentTable_PortScanNsqd" lay-filter="PortScanNsqd_Filter"></table>

            </div>
        </fieldset>

    </div>
</div>
<script src="/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            element = layui.element,
            table = layui.table;

        table.render({
            elem: '#currentTable_portscanmanager',
            url: "/api/util/portscan",
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '刷新',
                layEvent: 'Refresh',
                icon: 'layui-icon-refresh-3'
            }],
            cols: [[
                {field: 'id', width: 60, title: 'ID', sort: true},
                {field: 'cus_name', width: 200, title: '任务名'},
                {field: 'host_num', width: 130, title: '主机数'},
                {field: 'scan_num', width: 130, title: '已扫描'},
                {field: 'create_at', width: 180, title: '添加时间'},
                {field: 'progress', title: '进度', align: 'center', templet: function(d){
                    var ScanColor = '';
                    var jindu = (d.scan_num * 100) / d.host_num
                    if(jindu < 20){
                        ScanColor='layui-bg-red'
                    }else if(jindu >= 20 && jindu < 60){
                        ScanColor='layui-bg-orange'
                    }else if(jindu >= 60 && jindu < 90){
                        ScanColor='layui-bg-green'
                    }else if(jindu >= 90){
                        ScanColor='layui-bg-blue'
                    }
                    return '<div class="layui-progress layui-progress-big" lay-showpercent="true"><div class="layui-progress-bar '+ScanColor+'" lay-percent="'+jindu.toFixed(2)+'%"></div></div><br>'
                    }},
                {title: '操作', fixed:"right", minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 20, 40, 60, 80, 100],
            limit: 20,
            page: true,
            skin: 'line',
            done:function (res,currentCount) {
                element.render()
            }
        });

        table.render({
            elem: '#currentTable_PortScanNsqd',
            url: "/api/util/portscan/nsq",
            toolbar: '#toolbarPortScanNsqd',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'Refresh',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field: 'hostname', width: 300, title: '客户端'},
                {field: 'remote_address', width: 250, title: 'IP地址'},
                {field: 'message_count', width: 150, title: '消息数'},
                {field: 'finish_count', width: 150, title: '已处理'},
                {field: 'connect_ts', minWidth: 180, title: '连接时间'},
            ]],
            // 数据渲染回调
            done: function(res){
                $("#message_count").text("消息总数："+res.message_count + "条");
                $("#message_bytes").text("消息大小："+res.message_bytes);
                $("#client_count").text("客户端数："+res.client_count + "个");
                $("#timeout_count").text("消息超时："+res.timeout_count + "条");
            },
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);

            //执行搜索重载
            table.reload('currentTable_portscanmanager', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        // 定时15s刷新信息
        function callbackNsqd(){
            table.reload('currentTable_PortScanNsqd', {
                page: {
                    curr: 1
                }
                ,
            }, 'data');
            table.reload('currentTable_portscanmanager', {
                page: {
                    curr: 1
                }
                ,
            }, 'data');
        }
        setInterval(callbackNsqd, 15000);

        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加端口扫描任务',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: "/util/portscan/manager/add",
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }else if(obj.event === 'Refresh'){
                table.reload('currentTable_portscanmanager', {
                    page: {
                        curr: 1
                    }
                    ,
                }, 'data');
            }else if(obj.event === 'empty'){
                layer.confirm('确定清空所有端口扫描的数据吗？该操作会清空已有任务及端口扫描的数据', function (index) {
                    $.ajax({
                        url: "/api/util/portscan",
                        data: "",
                        contentType: 'application/json;charset=UTF-8',
                        dataType: 'json',
                        type: 'put',
                        success: function (result) {
                            if (result.code == 200) {
                                layer.alert('清空所有端口扫描数据成功!', {title: '提示', icon: 1})
                                layer.close(index);
                                table.reload('currentTable_portscanmanager', {
                                    page: {
                                        curr: 1
                                    }
                                    ,
                                }, 'data');
                            } else {
                                layer.alert(result.msg, {title: '提示', icon: 2})
                            }
                        },
                        error: function (e) {
                            layer.alert(JSON.stringify(e), {title: '提示', icon: 2})
                        }
                    })
                });
            }
        });

        // 删除任务
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'delete') {
                layer.confirm('确定删除该任务吗?删除该任务不影响消息队列中数据，所以请确保扫描完毕后删除，或者清除后再清空任务队列', function (index) {
                    $.ajax({
                        url: "/api/util/portscan",
                        data: JSON.stringify(data),
                        contentType: 'application/json;charset=UTF-8',
                        dataType: 'json',
                        type: 'delete',
                        success: function (result) {
                            if (result.code == 200) {
                                layer.alert('删除任务成功!', {title: '提示', icon: 1})
                                obj.del();
                                layer.close(index);
                            } else {
                                layer.alert(result.msg, {title: '提示', icon: 2})
                            }
                        },
                        error: function (e) {
                            layer.alert(JSON.stringify(e), {title: '提示', icon: 2})
                        }
                    })
                });
            }else if(obj.event === "show"){
                var TaskName = obj.data.cus_name;
                localStorage.setItem("task_name", TaskName);
                var index = layer.open({
                    title: TaskName+" 端口扫描详情",
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: "/util/portscan/manager/show",
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

    });
</script>
</body>
</html>
